package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

//Class for browsing in folders
public class LoadMaps extends MainPanel{

	private static final long serialVersionUID = 1L;
	private JScrollPane loadScrollPane;
	private File[] files;
	private int imageNr;
	private JPanel loadMapsPanel;
	private JPanel buttonPanel;
	private GameButton backToMainMenu;
	private GameButton startGame;
	private GameFolderItem gameLoad[];
	private MainPanel parent;
	
	//constructor whereby we can set in which folder we want to view the files 
	public LoadMaps(File dir) {
		setLayout(new BorderLayout());
		buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		startGame = new GameButton("Indit");
		backToMainMenu = new GameButton("Vissza");
		buttonPanel.add(startGame);
		buttonPanel.add(backToMainMenu);
		buttonPanel.setBackground(new Color(91, 88, 66));
		add(buttonPanel, BorderLayout.SOUTH);
		if( dir.exists() && dir.isDirectory()){
			files = dir.listFiles();
			if(files.length % 4 == 0)
				imageNr = files.length / 4;
			else
				imageNr = files.length / 4 + 1;
			loadMapsPanel = new JPanel(null);
			loadMapsPanel.setPreferredSize(new Dimension(782, 10+imageNr*(137+10)) );
			loadScrollPane = new JScrollPane(loadMapsPanel);
			loadScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			gameLoad = new GameFolderItem[files.length];
			int x = 0;
			int y = 0;
			for(int i=0; i < files.length; i++){
				if(files[i].isDirectory()){
					gameLoad[i] = new GameFolderItem(this, 10 + (183 + 10)*x, 10+(137+10)*y, files[i]);
					loadMapsPanel.add(gameLoad[i]);
					if(x == 3){
						x = 0;
						y++;
					}else
						x++;
				}
					
			}
			for(int i=0; i < files.length; i++){
				if(files[i].isFile()){
					gameLoad[i] = new GameFolderItem(this, 10 + (183 + 10)*x, 10+(137+10)*y, files[i]);
					loadMapsPanel.add(gameLoad[i]);
					if(x == 3){
						x = 0;
						y++;
					}else
						x++;
				}
			}
		}
		else{
			JOptionPane.showMessageDialog(null, "Nem letezo mappa!");
			loadMapsPanel = new JPanel(null);
			loadScrollPane = new JScrollPane(loadMapsPanel);
		}
		add(loadScrollPane, BorderLayout.CENTER);
		loadMapsPanel.setBackground(new Color(91, 88, 66));
		backToMainMenu.addActionListener(new ClickedOnBackButton());
	}
	
	public JPanel getDirectory(){
		return this.loadMapsPanel;
	}
	
	//with this method we can set the redirection to panel of clicking on back button
	public void setParent(MainPanel parent){
		this.parent = parent;
	}

	// inner class for listening for clicking on back button
	public class ClickedOnBackButton implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			MainWindow.getMainWindow().setContentPane(parent);
			validate();
		}
		
	}
}